import os
import random
import string
import time
from datetime import datetime
# 获取当前脚本所在的绝对路径
current_path = os.path.abspath(__file__)
# 获取根目录
root_dir = os.path.dirname(current_path)

def random_string():
    random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
    return random_string

def check_ip(section):
    import socket
    from bin.conf import ConfigManager
    config_manager = ConfigManager('./conf/base.ini')
    cn = config_manager.get_config()
    ip = cn[section]['url']
    try:
        socket.inet_aton(ip)  # 尝试将IP地址转换为二进制数据
        print(f"{ip} is a valid IP address.")
        # 你可以在这里添加代码来测试网络连接，例如使用 `ping` 命令
        response = os.system(f"ping -c 1 {ip} > nul")

    except:
        print(f"{ip} is not a valid IP address.")
        return False

def time_strap():
    # 获取当前时间的Unix时间戳
    timestamp = time.time()
    # 将时间戳转换为datetime对象
    dt = datetime.fromtimestamp(timestamp)
    # 获取年月日并格式化输出
    year, month, day = dt.year, dt.month, dt.day
    formatted_date = f"{year}-{month:02d}-{day:02d}"
    return formatted_date

